package com.haoze.nurseapp.controller;

import com.haoze.common.response.Result;
import com.haoze.common.response.ResultGenerator;
import com.haoze.nurseapp.dto.Notification;
import com.haoze.nurseapp.service.NurseTeamService;
import com.haoze.nurseapp.vo.NurseTeamDutyVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/nurseapp/nurseteam")
@Api(value = "护士分组", tags = "护士分组")
public class NurseTeamController {

    @Autowired
    NurseTeamService nurseTeamService;

    @GetMapping("/findPositionByUser")
    @ApiOperation(value = "验证当前用户是否是护士长")
    public Result findPositionByUser() {
        return nurseTeamService.findPositionByUser();
    }

    @GetMapping("/teamList")
    @ApiOperation(value = "查询分组")
    @ApiImplicitParam(name = "wardDeptId", value = "病区id", dataType = "String", paramType = "query", example = "", required = true)
    public Result teamList(@RequestParam("wardDeptId") String wardDeptId) {
        return nurseTeamService.teamList(wardDeptId);
    }

    @GetMapping("/getListForInsert")
    @ApiOperation(value = "获取护士信息和床号信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wardDeptId", value = "病区号", dataType = "String", paramType = "query", example = "", required = true),
            @ApiImplicitParam(name = "teamId", value = "组ID", dataType = "String", paramType = "query", example = "", required = false)
    })
    public Result getListForInsert(@RequestParam("wardDeptId") String wardDeptId, @RequestParam(value = "teamId", required = false) String teamId) {
        return nurseTeamService.getListForInsert(wardDeptId, teamId);
    }

    @PostMapping("/addOrUpdate")
    @ApiOperation(value = "新增或修改")
    public Result addOrUpdate(@RequestBody NurseTeamDutyVo nurseTeamDutyVo) {
        return nurseTeamService.addOrUpdate(nurseTeamDutyVo);
    }

    @GetMapping("/deleteGroup")
    @ApiOperation(value = "删除分组")
    public Result deleteGroup(@RequestParam("teamId") String teamId) {
        return nurseTeamService.deleteGroup(teamId);
    }

    @GetMapping("/queryMyPatientList")
    @ApiOperation(value = "护士进入聊天界面，查询我的病人列表/科室病人，并带出最新的消息")
    public Result queryPatientList(@RequestParam(value = "type") String type, @RequestParam(value = "wardDeptId") String wardDeptId, @RequestParam("page") int page, @RequestParam("pageSize") int pageSize, @RequestParam(value = "searchValue",required = false) String searchValue) {
        return nurseTeamService.queryMyPatientList(type, wardDeptId, page, pageSize, searchValue);
    }

    @GetMapping("/queryMyPatientForDoctList")
    @ApiOperation(value = "医生进入聊天界面，查询我的病人列表/科室病人，并带出最新的消息")
    public Result queryMyPatientForDoctList(@RequestParam(value = "type") String type, @RequestParam(value = "wardDeptId") String wardDeptId, @RequestParam("page") int page, @RequestParam("pageSize") int pageSize, @RequestParam(value = "searchValue",required = false) String searchValue) {
        return nurseTeamService.queryMyPatientForDoctList(type, wardDeptId, page, pageSize, searchValue);
    }

    @GetMapping("/queryPatientHistoryMsgList")
    @ApiOperation(value = "护士进入聊天界面，点击我的病人中的某一个人，查询出历史聊天记录")
    public Result queryPatientHistoryMsgList(@RequestParam("inpatNum") String inpatNum, @RequestParam(value = "type", required = false) String type, @RequestParam("page") int page, @RequestParam("pageSize") int pageSize, @RequestParam(value = "searchValue", required = false) String searchValue, @RequestParam(value = "searchStatus", required = false) String searchStatus) {
        return nurseTeamService.queryPatientHistoryMsgList(inpatNum, type, page, pageSize, searchValue, searchStatus);
    }

    @PostMapping("/queryMsgListByPatList")
    @ApiOperation(value = "异步加载每个病人的最新消息和未读消息数量")
    public Result queryMsgListByPatList(@RequestBody List<NurseTeamDutyVo> msgList) {
        return nurseTeamService.queryMsgListByPatList(msgList);
    }

    @GetMapping("/saveNurseStatus")
    @ApiOperation(value = "记录这个护士最近的选择病区是哪个病区还是选择我的病人，优先按照护士选择的病区进行发送")
    public Result saveNurseStatus(@RequestParam("wardDeptId") String wardDeptId, @RequestParam(value = "channle", required = false, defaultValue = "my") String channle) {
        return nurseTeamService.saveNurseStatus(wardDeptId, channle);
    }

    @GetMapping("/doAfterReadMsg")
    @ApiOperation(value = "读取消息后，把mongo里的消息isMsgRead都置为true")
    public Result doAfterReadMsg(@RequestParam("inpatNum") String inpatNum) {
        return nurseTeamService.doAfterReadMsg(inpatNum);
    }

    @GetMapping("/querySendUser")
    @ApiOperation(value = "查询这条消息要发送给哪些人")
    public Result querySendUser(@RequestParam("inpatNum") String inpatNum, @RequestParam("userType") String userType) {
        return ResultGenerator.genOkResult(nurseTeamService.querySendUser(inpatNum, userType));
    }

    @PostMapping("/saveMessage")
    @ApiOperation(value = "保存护士端发送的消息")
    public Result saveMessage(@RequestBody Map<String,Object> notifications) {
        return nurseTeamService.saveMessage(notifications);
    }

    @PostMapping("/transferMsg")
    @ApiOperation(value = "前台流转信息")
    public Result transferMsg(@RequestBody Map<String,Object> notifications) {
        return nurseTeamService.transferMsg(notifications);
    }

    @GetMapping("/getUserId")
    @ApiOperation(value = "获取当前用户的userId")
    public Result getUserId() {
        return ResultGenerator.genOkResult(nurseTeamService.getUserId());
    }
}
